package com.zs.grain

import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.widget.Toast
import com.example.clockkotlin.databaseClockAlarm.AlarmSignal
import com.jeremyliao.liveeventbus.LiveEventBus
import com.zs.grain.device.GrainDevice
import com.zs.grain.service.DataService
import com.zs.grain.service.DeviceService

import com.zs.grain.service.SoundService
import com.zs.grain.tool.clockManage.LocalDataBase
import com.zslibrary.BusMaster.BusMaster
import com.zslibrary.Enum.ComponentClassDefine.C_AUXILIARY
import java.util.*
import java.util.logging.Handler
import javax.inject.Inject
import kotlin.collections.ArrayList
import kotlin.concurrent.timerTask

class UVLightReceiver: BroadcastReceiver() {
    lateinit var mGrainDevice: GrainDevice
    //@Inject
    // = null
    //private var mSoundService: SoundService?=null
    override fun onReceive(context: Context?, intent: Intent?) {
        mGrainDevice = GrainDevice(context!!)
        val clocksArray:ArrayList<AlarmSignal> = LocalDataBase.getClocksArray()
        for(ca in clocksArray){
            if(ca.setting.contains("uvlight")){
                val min = ca.setting.substring(8).toInt()
                setUVLightStart(context,min)
            }
        }
    }

    fun setUVLightStart(context: Context?,minAfterClose:Int){
        //if(rebootEnable) {
            Toast.makeText(context,"机器即将进入消毒,UV灯自动打开...",Toast.LENGTH_LONG).show();
            //startUVLight();
            mGrainDevice.testComponent(2001,2)
//            BusMaster(context).componentTest(C_AUXILIARY+1,2)
            val timer = Timer()
            timer.schedule(timerTask {
                //closeUVLight();
                mGrainDevice.testComponent(2001,3)
                //BusMaster(context).componentTest(C_AUXILIARY+1,3)
//                val componetName = ComponentName(
//                    "com.example.reboottest",
//                    "com.example.reboottest.MainActivity"
//                );
//                val intent = Intent();
//                intent.setComponent(componetName);
//                context?.startActivity(intent);
            }, (60*1000*minAfterClose).toLong())
        //}
    }
}